<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body class="layui-layout-body">
    <div class="layui-layout layui-layout-admin">
        <!-- 头部区域 -->
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">小药店管理系统</div>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a href="javascript:;">
                        <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                        欢迎, <span id="username">管理员</span>
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;" onclick="logout()">退出</a></dd>
                    </dl>
                </li>
            </ul>
        </div>

        <!-- 左侧导航区域 -->
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item">
                        <a href="javascript:;">采购管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../supplier/supplier.html">供应商信息管理</a></dd>
                            <dd><a href="../supplier/drug.html">药品信息管理</a></dd>
                            <dd><a href="../procurement/add.html">采购单添加</a></dd>
                            <dd><a href="../procurement/list.html">采购单管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">销售管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../sales/add_xiaoshoudan.html">销售单添加</a></dd>
                            <dd><a href="../sales/xiaoshoumingxi.html">销售单管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">药店统计管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../statistics/instock.html">入库统计</a></dd>
                            <dd><a href="../statistics/sales.html">销售统计</a></dd>
                            <dd><a href="../statistics/stock.html">库存查询</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item layui-nav-itemed">
                        <a href="javascript:;">人事管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="add_yuangong.html">员工添加</a></dd>
                            <dd><a href="yuangong.html">员工管理</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">打印管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../print/ruku.html">入库单</a></dd>
                            <dd><a href="../print/chuku.html">出库单</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">系统管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="../user/xiuGaiMiMa.html">修改密码</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>

        <!-- 内容主体区域 -->
        <div class="layui-body">
            <div style="padding: 15px;">
                <!-- 面包屑导航 -->
                <div class="layui-card">
                    <div class="layui-card-body">
                        <span class="layui-breadcrumb">
                            <a href="../index.html">首页</a>
                            <a><cite>员工管理</cite></a>
                        </span>
                    </div>
                </div>

                <!-- 搜索栏 -->
                <div class="layui-card">
                    <div class="layui-card-body">
                        <form class="layui-form" lay-filter="searchForm">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">姓名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" placeholder="请输入姓名" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">职位</label>
                                    <div class="layui-input-inline">
                                        <select name="position">
                                            <option value="">全部</option>
                                            <option value="店长">店长</option>
                                            <option value="销售员">销售员</option>
                                            <option value="采购员">采购员</option>
                                            <option value="仓管员">仓管员</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn" lay-submit lay-filter="searchForm">
                                        <i class="layui-icon layui-icon-search"></i> 搜索
                                    </button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    <a href="add_yuangong.html" class="layui-btn layui-btn-normal">
                                        <i class="layui-icon layui-icon-add-1"></i> 添加员工
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 数据表格 -->
                <div class="layui-card">
                    <div class="layui-card-body">
                        <table id="employeeTable" lay-filter="employeeTable"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 表格操作列模板 -->
    <script type="text/html" id="operationBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">离职</a>
    </script>

    <!-- 状态列模板 -->
    <script type="text/html" id="statusTpl">
        {{#  if(d.status === 1){ }}
            <span class="layui-badge layui-bg-green">在职</span>
        {{#  } else { }}
            <span class="layui-badge layui-bg-gray">离职</span>
        {{#  } }}
    </script>

    <script src="/layui/layui.js"></script>
    <script>
        layui.use(['table', 'form', 'layer'], function(){
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.jquery;

            // 初始化表格
            table.render({
                elem: '#employeeTable',
                url: '/employee/list',
                page: true,
                parseData: function(res) {
                    return {
                        "code": 0,
                        "msg": res.msg || '',
                        "count": res.data.count,
                        "data": res.data.data
                    };
                },
                cols: [[
                    {field: 'id', title: 'ID', width: 80, sort: true},
                    {field: 'name', title: '姓名', width: 100},
                    {field: 'sex', title: '性别', width: 80},
                    {field: 'age', title: '年龄', width: 80},
                    {field: 'tel', title: '电话', width: 120},
                    {field: 'address', title: '地址'},
                    {field: 'position', title: '职位', width: 100},
                    {field: 'status', title: '状态', width: 100, templet: '#statusTpl'},
                    {title: '操作', width: 150, toolbar: '#operationBar', fixed: 'right'}
                ]],
                limits: [10, 20, 50, 100],
                limit: 10
            });

            // 监听搜索表单提交
            form.on('submit(searchForm)', function(data){
                table.reload('employeeTable', {
                    where: data.field,
                    page: {
                        curr: 1
                    }
                });
                return false;
            });

            // 监听工具条
            table.on('tool(employeeTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('确定要炒该员工鱿鱼吗？', function(index){
                        $.ajax({
                            url: '/employee/delete/' + data.id,
                            type: 'DELETE',
                            success: function(res){
                                if(res.code === 0){
                                    layer.msg('删除成功', {icon: 1});
                                    obj.del();
                                } else {
                                    layer.msg(res.msg || '删除失败', {icon: 2});
                                }
                            }
                        });
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        title: '编辑员工',
                        content: 'edit_yuangong.html?id=' + data.id,
                        area: ['800px', '600px'],
                        end: function(){
                            table.reload('employeeTable');
                        }
                    });
                }
            });
        });

        // 退出登录
        function logout() {
            layui.use(['jquery', 'layer'], function(){
                var $ = layui.$;
                var layer = layui.layer;
                
                $.ajax({
                    url: '/user/logout',
                    type: 'GET',
                    success: function(res) {
                        if(res.code === 0) {
                            window.location.href = '/login/login.html';
                        } else {
                            layer.msg(res.msg || '退出失败', {icon: 2});
                        }
                    },
                    error: function() {
                        layer.msg('服务器错误', {icon: 2});
                    }
                });
            });
        }
    </script>
</body>
</html> 